var updateFormHtml;
$(function() {
	$(".g-search").click(function() {
		var status = parseInt($("#search-model").val());
		var datagrid = $("#server-infos").datagrid('options');
		if (isNaN(status) || status < 1) {
			datagrid.url = "/backend/servermanage/nodelist/infos";
		} else {
			datagrid.url = "/backend/servermanage/nodelist/infosstatus/" + status;
		}
		$("#server-infos").datagrid('reload');
	});
	$("#server-infos").datagrid({
		fitColumns : true,
		width : 'auto',
		scrollbarSize : 0,
		url : '/backend/servermanage/nodelist/infos',
		method : 'GET',
		singleSelect : true,
		toolbar : '#model-action-toolbar',
		onLoadSuccess : function(data) {
			if (data.total == 0) {
				$('#server-infos').datagrid('appendRow', {
					name : '无记录'
				});
			}
		},
		columns : [ [ {
			field : 'name',
			title : '服务器名称',
			width : fixWidth(0.10)
		}, {
			field : 'serverName',
			title : '控制台显示名称',
			width : fixWidth(0.20)
		}, {
			field : 'ipaddress',
			title : 'IP地址',
			width : fixWidth(0.08)
		}, {
			field : 'domain',
			title : '对应域名',
			width : fixWidth(0.12)
		}, {
			field : 'loginPort',
			title : '分控登录端口',
			width : fixWidth(0.10)
		}, {
			field : 'status',
			title : '状态',
			align : 'center',
			width : fixWidth(0.04),
			formatter : function(value, row, index) {
				if (isNaN(parseInt(row.id))) {
					return '';
				}
				if (parseInt(value) === 1) {
					return '<span style="color: green;">正常</span>';
				} else if (parseInt(value) === 2) {
					return '<span style="color: orange;">关闭</span>';
				} else if (parseInt(value) === 3) {
					return '<span style="color: blue;">冻结</span>';
				} else if (parseInt(value) === 4) {
					return '<span style="color: red;">已删除</span>';
				}
			}
		}, {
			field : 'operation',
			title : '操作',
			align : 'center',
			width : fixWidth(0.05),
			formatter : function(value, row, index) {
				if (isNaN(parseInt(row.id))) {
					return '';
				}
				if (parseInt(row.status) === 4) {
					return '';
				}
				var html = '<span class="l-btn-icon icon-edit" onclick="actionedit(';
				html += row.id;
				html += ')" style="position: static; cursor: pointer;">&nbsp;</span>';

				if (parseInt(row.status) === 2) {
					html += '&nbsp;&nbsp; &nbsp;&nbsp;<span class="l-btn-icon icon-no"  onclick="actiondelet(';
					html += row.id;
					html += ')" style="position: static; cursor: pointer;">&nbsp;</span>';
				} else {
					html += '&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;';
				}
				return html;
			}
		} ] ]
	});
	$(window).on("resize", function() {
		$('#server-infos').datagrid('resize', {
			width : datagridResizeWidth()
		});
	});

	updateFormHtml = $("#append-form").html();
});

// 删除
function actiondelet(id) {
	var data = $("#server-infos").datagrid('getData');
	var rows = data.rows;
	var item;
	for (var i = 0; i < rows.length; i++) {
		if (parseInt(rows[i].id) === parseInt(id)) {
			item = rows[i];
		}
	}
	if (!item) {
		return;
	}
	layer.ready(function() {
		// 询问框
		layer.confirm('确认要删除？', {
			btn : [ '确认', '取消' ]
		// 按钮
		}, function(index) {
			request.post('/backend/servermanage/nodelist/delete', {
				id : item.id
			}, function(response, data) {
				if (data && data.id && parseInt(data.id) > 0) {
					$('#server-infos').datagrid('reload');
				}
			}, null, function() {
				layer.close(index);
			});
		});
	})
}

// 更新
function actionedit(id) {
	var data = $("#server-infos").datagrid('getData');
	var rows = data.rows;
	var item;
	for (var i = 0; i < rows.length; i++) {
		if (parseInt(rows[i].id) === parseInt(id)) {
			item = rows[i];
		}
	}
	if (!item) {
		return;
	}

	layer.ready(function() {
		layer.open({
			type : 1,
			title : '更新动作',
			skin : 'layui-layer-rim', // 加上边框
			area : [ '500px', '460px' ], // 宽高
			btn : [ '确定修改', '取消' ],
			content : updateFormHtml,
			success : function(layero, index) {
				$(".layui-layer-content input[name='name']").val(item.name);
				$(".layui-layer-content input[name='server_name']").val(item.serverName);
				$(".layui-layer-content input[name='ipaddress']").val(item.ipaddress);
				$(".layui-layer-content input[name='domain']").val(item.domain);
				$(".layui-layer-content input[name='login_port']").val(item.loginPort);
				$(".layui-layer-content select[name='status']").val(item.status);
			},
			btn1 : function(index, layero) {
				var param = updateFormServerData();
				if (!param) {
					return;
				}
				if ($(".layui-layer-content .mloading").hasClass("active")) {
					return;
				}
				param.id = item.id
				$(".layui-layer-content").mLoading("show");
				request.jpost('/backend/servermanage/nodelist/update', param, function(response, data) {
					if (data && data.id && parseInt(data.id) > 0) {
						$('#server-infos').datagrid('reload');
					}
				}, null, function() {
					$(".layui-layer-content").mLoading("hide");
					layer.close(index);
				});
			}
		})
	})
}

function updateFormServerData() {
	var name = $.trim($(".layui-layer-content input[name='name']").val());
	var server_name = $(".layui-layer-content input[name='server_name']").val();
	var ipaddress = $(".layui-layer-content input[name='ipaddress']").val();
	var domain = $(".layui-layer-content input[name='domain']").val();
	var login_port = $(".layui-layer-content input[name='login_port']").val();
	var status = $(".layui-layer-content select[name='status']").val();

	if (name === '') {
		mylayer.error('服务器名不能为空！');
		$("input[name='name']").focus();
		return;
	}
	if (name.length > 64) {
		mylayer.error('服务器名不能超过64个字符！');
		$("input[name='name']").focus();
		return;
	}

	if (server_name === '') {
		mylayer.error('主控控制台显示名称不能为空！');
		$("input[name='server_name']").focus();
		return;
	}

	if (server_name.length > 64) {
		mylayer.error('主控控制台显示名称不能超过64个字符！');
		$("input[name='server_name']").focus();
		return;
	}

	if (ipaddress === '') {
		mylayer.error('服务器IP地址不能为空！');
		$("input[name='ipaddress']").focus();
		return;
	}

	if (ipaddress.length > 64) {
		mylayer.error('服务器IP地址不能超过64个字符！');
		$("input[name='ipaddress']").focus();
		return;
	}

	if (domain === '') {
		mylayer.error('服务器域名不能为空！');
		$("input[name='domain']").focus();
		return;
	}

	if (domain.length > 64) {
		mylayer.error('服务器域名不能超过64个字符！');
		$("input[name='domain']").focus();
		return;
	}

	if (login_port === '') {
		mylayer.error('登录端口号不能为空！');
		$("input[name='login_port']").focus();
		return;
	}

	return {
		name : name,
		serverName : server_name,
		ipaddress : ipaddress,
		domain : domain,
		loginPort : login_port,
		status : status
	}
}